import { create } from 'dva-core';
import createLoading from 'dva-loading';
import { createLogger } from 'redux-logger';
import models from '../models'

let app;
let store;
let dispatch;

function createApp(opt) {
  app = create(opt);
  app.use(createLoading());
  // app.use(createLogger());
  if (!global.registered) opt.models.forEach(model => app.model(model));
  global.registered = true;
  app.start();
  store = app._store;
  app.getStore = () => store;
  app.use({ onError(err) {
    console.log(err);
  }});
  dispatch = store.dispatch;
  app.dispatch = dispatch;
  return app;
}

export default {
  createApp,
  getDispatch() {
    return app.dispatch;
  },
  getStore() {
    return app.getStore();
  }
}
